<?php

namespace Frieza\Modules\ContentCategory\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\service\ContentCategory;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\ContentCategory\ContentCategoryDimension;
use Doraemon\model\ContentCategory\ContentCategoryList;
use Illuminate\Support\MessageBag;
use Doraemon\model\ContentCategory\ContentCategorys;




class CategoryController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    //维度列表
    public function index(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('内容分类管理')
            ->breadcrumb(
                ['text' => '内容分类列表', 'url' => '/contentCategory/category/index']
            )
            ->body(self::listAction($request));


    }

    public function listAction($request = [])
    {
        $infos = ContentCategoryList::getList();
        $gridModel = new pGridDataModel($infos, count($infos), []);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disablePagination();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableFilter();

            $dimensions_arr = [];
            $dimensions = ContentCategoryDimension::getList(['id','dimension']);
            if($dimensions && is_array($dimensions)) {
                foreach($dimensions as $dimension) {
                    $dimensions_arr[$dimension['id']] = $dimension['dimension'];
                }
            }

            $grid->column('id');
            $grid->column('category', '分类');
            $grid->column('dimension_id', '所属维度')->replace($dimensions_arr);
            $grid->column('is_show', '展示状态')->replace([1=>'展示', 0 => '隐藏']);
            $grid->column('addtime', '添加时间');
        });

        $list = $listGrid->render();

        return $list;
    }

    public function add(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('内容分类管理')
            ->breadcrumb(
                ['text' => '内容分类管理', 'url' => '/contentCategory/category/index'],
                ['text' => '添加分类', 'url' => '/contentCategory/category/index/create']
            )
            ->body(self::editAction($request, $id = 0));

    }

    public function editAction($request, $id = 0)
    {
        $data = [];
        if($id == 0) {
            //添加
            $form = new Form($data);
        }

        $dimensions_arr = [];
        $dimensions = ContentCategoryDimension::getList(['id','dimension']);
        if($dimensions && is_array($dimensions)) {
            foreach($dimensions as $dimension) {
                $dimensions_arr[$dimension['id']] = $dimension['dimension'];
            }
        }

        $form->hidden('id');
        $form->text('category', '分类名称')->setWidth(4, 2)->required('不能为空')->help('请输入分类名称');
        $form->select('dimension_id', '所属维度')->options($dimensions_arr)->required('不能为空')->help('请选择维度');
        $form->action('/admin/contentCategory/category/index/save');

        return $form->render();
    }

    public function editSubmit(Request $request, Content $content)
    {
        $request = $request->all();
        $rs = false;

        $data['dimension_id'] = $request['dimension_id'];
        $data['category'] = $request['category'];
        $data['admin_id'] = Admin::user()->id;
        $data['category_status'] = ContentCategoryList::STATUS_NORMAL;
        $data['addtime'] = date('Y-m-d H:i:s', time());

        $rs = ContentCategoryList::addInfo($data);


        if($rs) {
            $success = new MessageBag([
                'title' => '提交成功'
            ]);
            return redirect('/admin/contentCategory/category/index')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '提交失败'
            ]);
            return redirect('/admin/contentCategory/category/index')->with(compact('error'));
        }


    }

    //标记分类
    public function setMark(Request $request)
    {
        $code = 1;
        $msg = '分类成功';

        try {
            $params = $request->all();
            $obj_id = $params['obj_id'];
            $obj_type = $params['obj_type'];
            $category = $params['tag'];
            $type = $params['type'];
            $dimension_id = $params['dimension_id'];

            $old_category = ContentCategorys::getCategory($obj_id, $dimension_id);

            $data["category"] = '';
            if($type == 'add') {
                $data["category"] = $category;
            }
            $data["obj_id"] = $obj_id;
            $data['obj_type'] = $obj_type;
            $data['admin_id'] = Admin::user()->id;


            if($old_category) {
                $data["edittime"] = date('Y-m-d H:i:s', time());
                //更新
                $conditions = ['obj_id' => $obj_id, 'dimension_id' =>$dimension_id];
                ContentCategory::editCategorys($conditions, $data);
            } else {
                $data["addtime"] = date('Y-m-d H:i:s', time());
                ContentCategory::addCategorys($data);
            }


        } catch (\Exception $e) {
            $code = 0;
            $msg = $e->getMessage();
        }


        return compact('code', 'msg');
    }


}